import Vue from 'vue'
import axios from 'axios';
// import VueAxios from 'vue-axios';
import router from './router'
import VueLazyLoad from 'vue-lazyload';
import VueCookie from 'vue-cookie';
import { Message } from 'element-ui';
import store from './store'
import App from './App.vue'
// import env from './env';

//mock开关
const mock = false
if (mock) {
  require('./mock/api')
}
//这里根据前端的跨域方式做相应的调整
axios.defaults.baseURL = '/api'
axios.defaults.timeout = 8000  //设置最长响应时间为8秒
//根据环境变量获取不同的请求地址
// axios.defaults.baseURL = env.baseURL
//接口错误拦截
axios.interceptors.response.use(function (response) {
  let res = response.data
  let path = location.hash
  if (res.status == 0) {   //0状态码是成功
    return res.data       //返回接口中data数据
  } else if (res.status == 10) {      //未登录的状态码 10
    if (path != '#/index') {
      window.location.href = '/#/login' //跳转到登陆界面
    }
    return Promise.reject(res)
  } else {
    Message.warning(res.msg);
    return Promise.reject(res);        //报错
  }
}, (error) => {
  let res = error.response
  Message.error(res.data.message);
  return Promise.reject(error)
})


// Vue.use(VueAxios, axios);
Vue.prototype.axios = axios;
Vue.use(VueCookie);

// Vue.prototype.$message=Message;    将message挂载到vue实例上 调用的时候不用再import组件只需this.$message.xx（）使用
//Vue.use(Message)

Vue.config.productionTip = false   //关闭生产提示
Vue.use(VueLazyLoad, {
  loading: '/imgs/loading-svg/loading-bars.svg'
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')


router.beforeEach((to, from, next) => {
  // chrome
  document.body.scrollTop = 0
  // firefox
  document.documentElement.scrollTop = 0
  // safari
  window.pageYOffset = 0
  next()
})